/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-  */
/*
 * catchit
 * Copyright (C) 2013 Ryan Grinkewitz <graphite@Toto>
 * 
 */

#include "physics-manager.h"


PhysicsManager* PhysicsManager::physicsManagerInstance = NULL; 
	 
PhysicsManager* PhysicsManager::getInstance()
{
	if (!physicsManagerInstance){ 
		physicsManagerInstance = new PhysicsManager;
	}
	 
	return physicsManagerInstance;
}

bool PhysicsManager::checkCollision(Jumper* jumper, 
                                    std::vector<Level*> levels){
	bool collision = false;
	
	//Calculate the bottom of the jumper collision box
	SDL_Rect* jumperBox = jumper->getCollisionBox ();
	
	for(int i = 0; i < levels.size(); i++){
		int levelSize = levels[i]->getLevelSize();
		for(int j = 0;j < levelSize;j++){
			Platform* platform = levels[i]->getPlatform (j);
			if(platform != NULL){
				SDL_Rect* platformBox = platform->getCollisionBox();
				collision = checkCollision (jumperBox, platformBox);
				if(collision){
					//set the current level location
					//this needs to be done by the return 
					//to make this reentrent
					//jumper->setLevel(i);
					//jumper->setPlatform(j);
					lastLevel = i;
					lastPlatform = j;
					//jumper->setIsOnPlatform(true);
					return collision;
				}
			}
		}		
	}

//	jumper->setLevel(-1);
//	jumper->setPlatform(-1);
//	jumper->setIsOnPlatform(false);
	return collision;
}

bool PhysicsManager::checkCollision(SDL_Rect* jumperBox, 
                                    SDL_Rect* platformBox){
	bool collision = true;

	int bottomJumper = jumperBox->y + jumperBox->h;
	int topJumper = jumperBox->y;
	int leftJumper = jumperBox->x;
	int rightJumper = jumperBox->x +jumperBox->w;

	int topPlatform = platformBox->y;
	int bottomPlatform = platformBox->y + platformBox->h;
	int leftPlatform = platformBox->x;
	int rightPlatform = platformBox->x +platformBox->w;

	if( bottomJumper <= topPlatform 
		|| topJumper >= bottomPlatform 
		|| rightJumper <= leftPlatform
		|| leftJumper >= rightPlatform ){ 
			collision = false; 
		}else{
			collision = true;
		}
	
	return collision;
}
